package com.example.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.example.bbs.entity.User;
import com.example.bbs.mapper.AskMapper;
import com.example.bbs.mapper.ResponseMapper;
import com.example.bbs.mapper.UserMapper;
import com.example.bbs.pojo.Ask;
import com.example.bbs.pojo.AskVO;
import com.example.bbs.pojo.Response;
import com.example.bbs.pojo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author MinXu
 * @since 2021/5/5
 * QQ   754647431
 */
@Controller
@RequestMapping("/ask")
public class  AskController {

    @Autowired
    AskMapper askMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    ResponseMapper responseMapper;

    AskVO askToAskVO(Ask ask) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id", 1);
        User user = userMapper.selectOne(userQueryWrapper);
        QueryWrapper<Response> responseQueryWrapper = new QueryWrapper<>();
        responseQueryWrapper.eq("aid", ask.getId());
        Integer count = responseMapper.selectCount(responseQueryWrapper);
        return new AskVO(ask, user, count);
    }

    @RequestMapping
    public String ask(Model model) {
        QueryWrapper<Ask> askQueryWrapper = new QueryWrapper<>();
        askQueryWrapper.orderByDesc("id");
        List<Ask> asks = askMapper.selectList(askQueryWrapper);
        ArrayList<AskVO> askVOS = new ArrayList<>();
        for (Ask ask : asks) {
            askVOS.add(askToAskVO(ask));
        }
        model.addAttribute("asks", askVOS);
        return "ask";
    }

    @RequestMapping("/detail2/{id}")
    public String detail2(@PathVariable("id") Integer id, Model model) {
        QueryWrapper<Ask> askQueryWrapper = new QueryWrapper<>();
        askQueryWrapper.eq("id", id);
        Ask ask = askMapper.selectOne(askQueryWrapper);

        QueryWrapper<Response> responseQueryWrapper = new QueryWrapper<>();
        responseQueryWrapper.eq("aid", ask.getId());
        responseQueryWrapper.orderByDesc("id");
        List<Response> responses = responseMapper.selectList(responseQueryWrapper);

        ArrayList<ResponseVO> responseVOS = new ArrayList<>();
        for (Response response : responses) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id", 1);
            User user = userMapper.selectOne(userQueryWrapper);
            responseVOS.add(new ResponseVO(response, user));
        }

        model.addAttribute("ask", askToAskVO(ask));
        model.addAttribute("responses", responseVOS);
        return "ask_detail2";
    }
    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, Model model) {
        QueryWrapper<Ask> askQueryWrapper = new QueryWrapper<>();
        askQueryWrapper.eq("id", id);
        Ask ask = askMapper.selectOne(askQueryWrapper);

        QueryWrapper<Response> responseQueryWrapper = new QueryWrapper<>();
        responseQueryWrapper.eq("aid", ask.getId());
        responseQueryWrapper.orderByDesc("id");
        List<Response> responses = responseMapper.selectList(responseQueryWrapper);

        ArrayList<ResponseVO> responseVOS = new ArrayList<>();
        for (Response response : responses) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id", 1);
            User user = userMapper.selectOne(userQueryWrapper);
            responseVOS.add(new ResponseVO(response, user));
        }

        model.addAttribute("ask", askToAskVO(ask));
        model.addAttribute("responses", responseVOS);
        return "ask_detail";
    }
    @RequestMapping("/resp")
    public String resp(Response response, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
         response.setRespId(user.getId());
        //response.setRespId(1);
        response.setDate(new Date());
        //System.out.println(response);
        responseMapper.insert(response);
        return "redirect:/ask/detail/"+response.getAid();
    }
    @RequestMapping("/add")
    public String add(Ask ask, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        // response.setRespId(user.getId());
        ask.setUid(ask.getUid());
        ask.setDate(new Date());
        askMapper.insert(ask);
        //System.out.println(ask);
        return "redirect:/ask/detail/"+ask.getId();
    }
}
